package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Admin;
import com.itheima.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@RestController
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 注册管理员
     *
     * @param admin
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        adminService.add(admin);
        return new Result("success", "操作成功");
    }   

    /**
     * 分配用户角色信息列表
     *
     * @param adminUid
     * @return
     */
    @PostMapping("/assign")
    public Result assign(@RequestParam("adminUid") String adminUid) {
        Map<String, Object> resultMap = adminService.assign(adminUid);
        return new Result("success", resultMap);
    }

    /**
     * 批量删除管理员
     *
     * @param adminUids
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestParam("adminUids") String[] adminUids) {
        adminService.deleteByAdminUids(adminUids);
        return new Result("success", "删除成功");
    }

    /**
     * 更新管理员信息
     *
     * @param admin
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Admin admin) {
        adminService.edit(admin);
        return new Result("success", "更新成功");
    }

    /**
     * 强退用户
     *
     * @return
     */
    @PostMapping("/forceLogout")
    public Result forceLogout() {
        return null;
        //todo 强退用户
    }

    /**
     * 分页查询管理员
     * @param keywords
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getList")
    public Result getList(@RequestParam("keywords") String keywords, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {
        Page<Admin>adminPage=adminService.findPage(currentPage,pageSize,keywords);
        PageResult<Admin> adminPageResult = new PageResult<>();
        adminPageResult.setRows(adminPage.getResult());
        adminPageResult.setTotal(adminPage.getTotal());
        return new Result("success",adminPageResult);
    }

    /**
     * 获取在线管理员列表
     * @return
     */
    @GetMapping("/getOnlineAdminList")
    public Result getOnlineAdminList(){

        //todo 获取在线管理员列表
        return null;
    }

    /**
     * 重置管理员密码
     * @param uid
     * @return
     */
    @PostMapping("/restPwd")
    public Result restPwd(@RequestParam("uid")String uid){
        adminService.restPwd(uid);
        return new Result("success","重置管理员密码成功");
    }

    /**
     * 修改管理员手机
     * @param uid
     * @param newInfo
     * @param validCode
     * @return
     */
    @PostMapping("/updateEmail")
    public Result updateEmail(@RequestParam("uid")String uid,@RequestParam("newInfo")String newInfo,@RequestParam("validCode")String validCode){
        return adminService.updateEmail(uid,newInfo,validCode);
    }
}

